package com.md.youyouhui;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.md.youyouhui.net.ApiServiceFac;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.ui.MainActivity;
import com.md.youyouhui.utils.DownLoadNotification;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.statistics.common.DeviceConfig;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.PlatformConfig;

import java.util.Objects;


/**
 * @author yawei
 * @data on 17-12-18  下午1:47
 * @project furling-a
 * @org www.komlin.com
 * @email zyawei@live.com
 */

public class App extends Application {
    private static final String TAG = "Application";
    private static Context context;
    private static int badge;

    private static String umengKey = "5e0d5f76cb23d23f1f0000e4";
    private static String pushSecrect = "5e0d5f76cb23d23f1f0000e4";
    //不同平台展示不同的channel
    private static String channel = "Umeng";
    private PushAgent mPushAgent;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        ApiServiceFac.init(context);






        //友盟
        UMConfigure.setLogEnabled(true);
        Log.e(TAG, " getTestDeviceInfo: " + getTestDeviceInfo(context)[0] + " " + getTestDeviceInfo(context)[1]);
        UMConfigure.init(context, umengKey, channel, UMConfigure.DEVICE_TYPE_PHONE, pushSecrect);
        // 支持在子进程中统计自定义事件
        UMConfigure.setProcessEvent(true);
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_AUTO);

        //腾讯bug集成
        initBugly();
        initUMengPush();

    }




    public void initUMengPush() {

        //UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "ac0fc92e803247c38e577c0a71d7808f");


        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        mPushAgent = PushAgent.getInstance( this );
        mPushAgent.setNoDisturbMode( 0, 0, 0, 0 ); //关闭免打扰时间
        mPushAgent.setDisplayNotificationNumber( 0 );//0-10 当参数为0时，表示不合并通知
        mPushAgent.setNotificationPlaySound( MsgConstant.NOTIFICATION_PLAY_SERVER ); //声音
        mPushAgent.setNotificationPlayLights( MsgConstant.NOTIFICATION_PLAY_SERVER );//呼吸灯
        mPushAgent.setNotificationPlayVibrate( MsgConstant.NOTIFICATION_PLAY_SERVER );//振动
        // mPushAgent.setDebugMode(false);
        //注册推送服务，每次调用register方法都会回调该接口



        mPushAgent.register( new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                Log.e(TAG, "注册友盟推送: " + "成功");

                SharedPreferencesUtils.saveString(getContext(),"deviceToken",deviceToken);;

                if (SharedPreferencesUtils.getBoolean(getContext(), "FirstLog", true )) {
                    SharedPreferencesUtils.saveBoolean(getContext(), "isOpenUpush", true );
                }

            }

            @Override
            public void onFailure(String s, String s1) {
                Log.e(TAG, "注册友盟推送: " + "失败");
//				SharedPreferences.Editor editor = preferences.edit();
                SharedPreferencesUtils.saveBoolean(getContext(), "isOpenUpush", false );

            }
        } );


        /**
         * 自定义行为的回调处理，参考文档：高级功能-通知的展示及提醒-自定义通知打开动作
         * UmengNotificationClickHandler是在BroadcastReceiver中被调用，故
         * 如果需启动Activity，需添加Intent.FLAG_ACTIVITY_NEW_TASK
         * */
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {


            }

            @Override
            public void launchApp(Context context, UMessage uMessage) {



                super.launchApp( context, uMessage );


            }
        };
        mPushAgent.setNotificationClickHandler( notificationClickHandler );

        //mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
        //注册推送服务 每次调用register都会回调该接口
       /* mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                LogUtils.e("device token: " + deviceToken);
                //sendBroadcast(new Intent(UPDATE_STATUS_ACTION));
            }

            @Override
            public void onFailure(String s, String s1) {
                LogUtils.e("register failed: " + s + " " + s1);
                //sendBroadcast(new Intent(UPDATE_STATUS_ACTION));
            }
        });*/
       /* new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).run();*/

    }



    {
        PlatformConfig.setWeixin(Constants.WXKEY, "9a35ec94eded1ce499afce53f6fd2543");
     //   PlatformConfig.setQQZone("1109600174", "c7394704798a158208a74ab60104f0ba");

    }

    public static String[] getTestDeviceInfo(Context context) {
        String[] deviceInfo = new String[2];
        try {
            if (context != null) {
                deviceInfo[0] = DeviceConfig.getDeviceIdForGeneral(context);
                deviceInfo[1] = DeviceConfig.getMac(context);
            }
        } catch (Exception e) {
        }
        return deviceInfo;
    }

    void initBugly() {
        Beta.enableHotfix = false;
//        Beta.smallIconId = R.mipmap.ic_launcher_round;
//        Beta.largeIconId = R.mipmap.ic_launcher_round;
//        Beta.defaultBannerId = R.mipmap.ic_launcher;
        Beta.canShowUpgradeActs.add(MainActivity.class);
        //Beta.canShowUpgradeActs.add(SettingActivity.class);
        Beta.enableNotification = true;
        Beta.autoCheckUpgrade = true;
        Beta.upgradeCheckPeriod = 3 * 1000;
        if (Objects.equals(BuildConfig.BUILD_TYPE, "beta")) {
            Beta.autoDownloadOnWifi = true;
        }
        if (BuildConfig.DEBUG) {
            Beta.showInterruptedStrategy = true;
        }
        Beta.canShowApkInfo = true;
        Beta.downloadListener = new DownLoadNotification(this);
        //Beta.tipsDialogLayoutId = R.layout.tips_dialog;
        //Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog;
        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(this);
        userStrategy.setAppChannel(BuildConfig.BUILD_TYPE);
        Bugly.setIsDevelopmentDevice(this, BuildConfig.DEBUG);
        Bugly.init(this, "261d4bb864", BuildConfig.DEBUG, userStrategy);

    }


    /**
     * 获取全局上下文
     */
    public static Context getContext() {
        return context;
    }


    public static int getBadge() {
        return badge;
    }

    public static void setBadge(int badge1) {
        badge = badge1;
    }

    /**
     * @param context
     * @return
     */
    public String getAppName(Context context) {
        try {
            PackageManager packageManager = getApplicationContext().getPackageManager();
            ApplicationInfo packageInfo = packageManager.getApplicationInfo(getPackageName(), 0);
            String applicationName = (String) packageManager.getApplicationLabel(packageInfo);
            return applicationName;
        } catch (Exception e) {
            return "";
        }

    }
}
